package com.ym;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

public class CheckData {

	/***
	 * 判断是否为中文
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isCn(String str) {

		if (str == null || str.equals("")) {
			return false;
		}
		Pattern pattern = Pattern.compile("^[\u4e00-\u9fa5]+$");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;

		}
		return true;
	}

	/***
	 * 判断是否为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumer(String str) {
		if (str == null || str.equals("")) {
			return false;
		}
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	/**
	 * @Title: isMoney
	 * @Description: 判断是否是金额
	 * @Param @param str
	 * @Param @return
	 * @Return boolean
	 * @Throws
	 */
	public static boolean isMoney(String str) {
		if (str == null || str.equals("")) {
			return false;
		}
		Pattern pattern = Pattern
				.compile("^(([1-9]\\d{0,9})|0)(\\.\\d{1,2})?$");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	/***
	 * 判断是否为字母
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isLetters(String str) {
		if (str == null || str.equals("")) {
			return false;
		}
		Pattern pattern = Pattern.compile("[a-zA-Z]+");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}
	/**
	 * @Description: 判断是否手机号码
	 * @Title: isMobileNO 
	 * @Author cym
	 * @param mobiles
	 * @Throws
	 */
	public static boolean isMobileNO(String mobiles){  
		Pattern p = Pattern.compile("^1[3|4|5|8][0-9]\\d{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}
	public static String utf8Togb2312(String str) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			switch (c) {
			case '+':
				sb.append(' ');
				break;
			case '%':
				try {
					sb.append((char) Integer.parseInt(
							str.substring(i + 1, i + 3), 16));
				} catch (NumberFormatException e) {
					throw new IllegalArgumentException();
				}
				i += 2;
				break;
			default:
				sb.append(c);
				break;
			}
		}
		// Undo conversion to external encoding
		String result = sb.toString();
		String res = null;
		try {
			byte[] inputBytes = result.getBytes("8859_1");
			res = new String(inputBytes, "UTF-8");
		} catch (Exception e) {
		}
		return res;
	}

}
